home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / upvar.test < prev    next >
Text File  |  1993-07-17  |  8KB  |  304 lines

  1. # Commands covered:  upvar
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/upvar.test,v 1.4 93/07/17 14:38:10 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. test upvar-1.1 {reading variables with upvar} {
  32.     proc p1 {a b} {set c 22; set d 33; p2}
  33.     proc p2 {} {upvar a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
  34.     p1 foo bar
  35. } {foo bar 22 33 abc}
  36. test upvar-1.2 {reading variables with upvar} {
  37.     proc p1 {a b} {set c 22; set d 33; p2}
  38.     proc p2 {} {p3}
  39.     proc p3 {} {upvar 2 a x1 b x2 c x3 d x4; set a abc; list $x1 $x2 $x3 $x4 $a}
  40.     p1 foo bar
  41. } {foo bar 22 33 abc}
  42. test upvar-1.3 {reading variables with upvar} {
  43.     proc p1 {a b} {set c 22; set d 33; p2}
  44.     proc p2 {} {p3}
  45.     proc p3 {} {
  46.     upvar #1 a x1 b x2 c x3 d x4
  47.     set a abc
  48.     list $x1 $x2 $x3 $x4 $a
  49.     }
  50.     p1 foo bar
  51. } {foo bar 22 33 abc}
  52. test upvar-1.4 {reading variables with upvar} {
  53.     set x1 44
  54.     set x2 55
  55.     proc p1 {} {p2}
  56.     proc p2 {} {
  57.     upvar 2 x1 x1 x2 a
  58.     upvar #0 x1 b
  59.     set c $b
  60.     incr b 3
  61.     list $x1 $a $b
  62.     }
  63.     p1
  64. } {47 55 47}
  65. test upvar-1.4 {reading array elements with upvar} {
  66.     proc p1 {} {set a(0) zeroth; set a(1) first; p2}
  67.     proc p2 {} {upvar a(0) x; set x}
  68.     p1
  69. } {zeroth}
  70.  
  71. test upvar-2.1 {writing variables with upvar} {
  72.     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
  73.     proc p2 {} {
  74.     upvar a x1 b x2 c x3 d x4
  75.     set x1 14
  76.     set x4 88
  77.     }
  78.     p1 foo bar
  79. } {14 bar 22 88}
  80. test upvar-2.2 {writing variables with upvar} {
  81.     set x1 44
  82.     set x2 55
  83.     proc p1 {x1 x2} {
  84.     upvar #0 x1 a
  85.     upvar x2 b
  86.     set a $x1
  87.     set b $x2
  88.     }
  89.     p1 newbits morebits
  90.     list $x1 $x2
  91. } {newbits morebits}
  92. test upvar-2.3 {writing variables with upvar} {
  93.     catch {unset x1}
  94.     catch {unset x2}
  95.     proc p1 {x1 x2} {
  96.     upvar #0 x1 a
  97.     upvar x2 b
  98.     set a $x1
  99.     set b $x2
  100.     }
  101.     p1 newbits morebits
  102.     list [catch {set x1} msg] $msg [catch {set x2} msg] $msg
  103. } {0 newbits 0 morebits}
  104. test upvar-2.4 {writing array elements with upvar} {
  105.     proc p1 {} {set a(0) zeroth; set a(1) first; list [p2] $a(0)}
  106.     proc p2 {} {upvar a(0) x; set x xyzzy}
  107.     p1
  108. } {xyzzy xyzzy}
  109.  
  110. test upvar-3.1 {unsetting variables with upvar} {
  111.     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
  112.     proc p2 {} {
  113.     upvar 1 a x1 d x2
  114.     unset x1 x2
  115.     }
  116.     p1 foo bar
  117. } {b c}
  118. test upvar-3.2 {unsetting variables with upvar} {
  119.     proc p1 {a b} {set c 22; set d 33; p2; lsort [info vars]}
  120.     proc p2 {} {
  121.     upvar 1 a x1 d x2
  122.     unset x1 x2
  123.     set x2 28
  124.     }
  125.     p1 foo bar
  126. } {b c d}
  127. test upvar-3.3 {unsetting variables with upvar} {
  128.     set x1 44
  129.     set x2 55
  130.     proc p1 {} {p2}
  131.     proc p2 {} {
  132.     upvar 2 x1 a
  133.     upvar #0 x2 b
  134.     unset a b
  135.     }
  136.     p1
  137.     list [info exists x1] [info exists x2]
  138. } {0 0}
  139. test upvar-3.4 {unsetting variables with upvar} {
  140.     set x1 44
  141.     set x2 55
  142.     proc p1 {} {
  143.     upvar x1 a x2 b
  144.     unset a b
  145.     set b 118
  146.     }
  147.     p1
  148.     list [info exists x1] [catch {set x2} msg] $msg
  149. } {0 0 118}
  150. test upvar-3.5 {unsetting array elements with upvar} {
  151.     proc p1 {} {
  152.     set a(0) zeroth
  153.     set a(1) first
  154.     set a(2) second
  155.     p2
  156.     array names a
  157.     }
  158.     proc p2 {} {upvar a(0) x; unset x}
  159.     p1
  160. } {1 2}
  161. test upvar-3.6 {unsetting then resetting array elements with upvar} {
  162.     proc p1 {} {
  163.     set a(0) zeroth
  164.     set a(1) first
  165.     set a(2) second
  166.     p2
  167.     list [array names a] [catch {set a(0)} msg] $msg
  168.     }
  169.     proc p2 {} {upvar a(0) x; unset x; set x 12345}
  170.     p1
  171. } {{0 1 2} 0 12345}
  172.  
  173. test upvar-4.1 {nested upvars} {
  174.     set x1 88
  175.     proc p1 {a b} {set c 22; set d 33; p2}
  176.     proc p2 {} {global x1; upvar c x2; p3}
  177.     proc p3 {} {
  178.     upvar x1 a x2 b
  179.     list $a $b
  180.     }
  181.     p1 14 15
  182. } {88 22}
  183. test upvar-4.2 {nested upvars} {
  184.     set x1 88
  185.     proc p1 {a b} {set c 22; set d 33; p2; list $a $b $c $d}
  186.     proc p2 {} {global x1; upvar c x2; p3}
  187.     proc p3 {} {
  188.     upvar x1 a x2 b
  189.     set a foo
  190.     set b bar
  191.     }
  192.     list [p1 14 15] $x1
  193. } {{14 15 bar 33} foo}
  194.  
  195. proc tproc {args} {global x; set x [list $args [uplevel info vars]]}
  196. test upvar-5.1 {traces involving upvars} {
  197.     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
  198.     proc p2 {} {upvar c x1; set x1 22}
  199.     set x ---
  200.     p1 foo bar
  201.     set x
  202. } {{x1 {} w} x1}
  203. test upvar-5.2 {traces involving upvars} {
  204.     proc p1 {a b} {set c 22; set d 33; trace var c rw tproc; p2}
  205.     proc p2 {} {upvar c x1; set x1}
  206.     set x ---
  207.     p1 foo bar
  208.     set x
  209. } {{x1 {} r} x1}
  210. test upvar-5.3 {traces involving upvars} {
  211.     proc p1 {a b} {set c 22; set d 33; trace var c rwu tproc; p2}
  212.     proc p2 {} {upvar c x1; unset x1}
  213.     set x ---
  214.     p1 foo bar
  215.     set x
  216. } {{x1 {} u} x1}
  217.  
  218. test upvar-6.1 {retargeting an upvar} {
  219.     proc p1 {} {
  220.     set a(0) zeroth
  221.     set a(1) first
  222.     set a(2) second
  223.     p2
  224.     }
  225.     proc p2 {} {
  226.     upvar a x
  227.     set result {}
  228.     foreach i [array names x] {
  229.         upvar a($i) x
  230.         lappend result $x
  231.     }
  232.     lsort $result
  233.     }
  234.     p1
  235. } {first second zeroth}
  236. test upvar-6.2 {retargeting an upvar} {
  237.     set x 44
  238.     set y abcde
  239.     proc p1 {} {
  240.     global x
  241.     set result $x
  242.     upvar y x
  243.     lappend result $x
  244.     }
  245.     p1
  246. } {44 abcde}
  247. test upvar-6.3 {retargeting an upvar} {
  248.     set x 44
  249.     set y abcde
  250.     proc p1 {} {
  251.     upvar y x
  252.     lappend result $x
  253.     global x
  254.     lappend result $x
  255.     }
  256.     p1
  257. } {abcde 44}
  258.  
  259. test upvar-7.1 {upvar to same level} {
  260.     set x 44
  261.     set y 55
  262.     catch {unset uv}
  263.     upvar #0 x uv
  264.     set uv abc
  265.     upvar 0 y uv
  266.     set uv xyzzy
  267.     list $x $y
  268. } {abc xyzzy}
  269. test upvar-7.2 {upvar to same level} {
  270.     set x 1234
  271.     set y 4567
  272.     proc p1 {x y} {
  273.     upvar 0 x uv
  274.     set uv $y
  275.     return "$x $y"
  276.     }
  277.     p1 44 89
  278. } {89 89}
  279. test upvar-7.3 {upvar to same level} {
  280.     set x 1234
  281.     set y 4567
  282.     proc p1 {x y} {
  283.     upvar #1 x uv
  284.     set uv $y
  285.     return "$x $y"
  286.     }
  287.     p1 xyz abc
  288. } {abc abc}
  289.  
  290. test upvar-8.1 {errors in upvar command} {
  291.     list [catch upvar msg] $msg
  292. } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
  293. test upvar-8.2 {errors in upvar command} {
  294.     list [catch {upvar 1} msg] $msg
  295. } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
  296. test upvar-8.3 {errors in upvar command} {
  297.     proc p1 {} {upvar a b c}
  298.     list [catch p1 msg] $msg
  299. } {1 {wrong # args: should be "upvar ?level? otherVar localVar ?otherVar localVar ...?"}}
  300. test upvar-8.4 {errors in upvar command} {
  301.     proc p1 {} {set a 33; upvar b a}
  302.     list [catch p1 msg] $msg
  303. } {1 {variable "a" already exists}}
  304.